{% extends 'al120/base.html' %}
{% block content %}
    <form id="ff">
        {% csrf_token %}
        <div class="form-group row">
            <label for="id_time" class="col-sm-2 col-form-label">检验时间</label>
            <input type="date" name="sample_date" class="form-control col-sm-6"
                   id="id_time">
        </div>
        <button id="btn_query" type="submit" class="btn btn-info">查询</button>
        <button id="btn_excel" type="button" class="btn btn-info">导出</button>
    </form>
    {#查询值#}
    <br/>
    <div id="sample"></div>
{% endblock %}
{% block script %}
    <script>
        let data_store = null;
        $(document).ready(function () {
            let csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();

            function csrfSafeMethod(method) {
                // these HTTP methods do not require CSRF protection
                return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
            }

            $.ajaxSetup({
                beforeSend: function (xhr, settings) {
                    if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                        xhr.setRequestHeader("X-CSRFToken", csrftoken);
                    }
                }
            });

            // 查询日期默认今天
            let dt = moment().format('YYYY-MM-DD');
            $('#id_time').val(dt);
            //
            $('#ff').submit(function (evt) {
                evt.preventDefault();
                $('#sample').html('');
                $.post(
                    '{% url "al120:view" %}',
                    $('#ff').serialize(),
                    function (data) {
                        console.log(data);
                        if (data.status) {
                            $('#sample').html(data.html);
                            data_store = data.data;
                            console.log(data_store);
                        } else {
                            alert('无分析记录');
                        }

                    }
                );
            });

        });

        // 获取xls文件
        $('#btn_excel').click(function (evt) {
            let m = $('#id_month').val();
            if (data_store == null) {
                alert('请先查询');
                return;
            }
            // console.log(m);
            window.location.href = "/al/xls/?data=" + escape(JSON.stringify(data_store));
        });
    </script>

{% endblock %}